﻿// Copyright (c) Microsoft Corporation.  All rights reserved. 
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace WiE.Model
{
    class DeviceChangedArgs : DataObjectChangedArgs
    {
        public DeviceChangedArgs() { }

        public DeviceChangedArgs(WiEDeviceDataObject p_deviceDataObject) : base(p_deviceDataObject) { }

        public DeviceChangedArgs(WiEDeviceDataObject p_deviceDataObject, ChangeType p_changeType) : base(p_deviceDataObject, p_changeType) { }

        WiEDeviceDataObject DeviceChanged
        {
            set { base.m_wieDataObject = value; }
            get { return (WiEDeviceDataObject)base.m_wieDataObject; }
        }
    }

    [DataContract]
    class WiEDeviceDataObject : WiEDataObject
    {
        // Property Key definitions when Member Data Object for use when object is converted into a property bag...
        public const string KEY_DEVICEID = "DeviceID";
        public const string KEY_MEMBERID = "MemberID";
        public const string KEY_CLIENTSTARTDATE = "MobileClientStartDate";
        public const string KEY_CLIENTHEARTBEAT = "MobileClientHeartbeatDate";
        public const string KEY_LASTLOCATIONDATECOLLECTED = "LastLocationDateCollected";
        public const string KEY_LASTLOCATIONLATITUDE = "LastLocationLatitude";
        public const string KEY_LASTLOCATIONLONGITUDE = "LastLocationLongitude";

        // Member / properties
        private System.Guid m_guidDeviceID;
        private System.Guid m_guidMemberID;
        private DateTime? m_dateTimeClientStartUTC;
        private DateTime? m_dateTimeClientHeartBeatUTC;
        private DateTime? m_dateTimeLastLocationDateCollectedUTC;
        private Double? m_flLastLocationLatitude;
        private Double? m_flLastLocationLongitude;

        // constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public WiEDeviceDataObject()
        {
        }

        /// <summary>
        /// Returns the unique id for this object (Device).
        /// </summary>
        public override Guid UniqueId
        {
            get { return m_guidDeviceID; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_propertyBag"></param>
        public WiEDeviceDataObject(Dictionary<string, object> p_propertyBag)
        {
            foreach (KeyValuePair<string, object> kvp in p_propertyBag)
            {
                switch (kvp.Key)
                {
                    case KEY_DEVICEID:
                        m_guidDeviceID = new Guid(kvp.Value.ToString());
                        break;
                    case KEY_MEMBERID:
                        m_guidMemberID = new Guid(kvp.Value.ToString());
                        break;
                    case KEY_CLIENTSTARTDATE:
                        m_dateTimeClientStartUTC = DateTime.Parse(kvp.Value.ToString());
                        break;
                    case KEY_CLIENTHEARTBEAT:
                        m_dateTimeClientHeartBeatUTC = DateTime.Parse(kvp.Value.ToString());
                        break;
                    case KEY_LASTLOCATIONDATECOLLECTED:
                        m_dateTimeLastLocationDateCollectedUTC = DateTime.Parse(kvp.Value.ToString());
                        break;
                    case KEY_LASTLOCATIONLATITUDE:
                        m_flLastLocationLatitude = Double.Parse(kvp.Value.ToString());
                        break;
                    case KEY_LASTLOCATIONLONGITUDE:
                        m_flLastLocationLongitude = Double.Parse(kvp.Value.ToString());
                        break;
                }
            }
        }

        [DataMember]
        public Guid DeviceID
        {
            set { m_guidDeviceID = value; }
            get { return m_guidDeviceID; }
        }

        [DataMember]
        public Guid MemberID
        {
            set { m_guidMemberID = value; }
            get { return m_guidMemberID; }
        }

        [DataMember]
        public DateTime? ClientStarted
        {
            set {m_dateTimeClientStartUTC = value; }
            get { return m_dateTimeClientStartUTC; }
        }

        [DataMember]
        public DateTime? LastLocationDateCollected
        {
            set { m_dateTimeLastLocationDateCollectedUTC = value; }
            get { return m_dateTimeLastLocationDateCollectedUTC; }
        }

        [DataMember]
        public DateTime? ClientHeartbeat
        {
            set { m_dateTimeClientHeartBeatUTC = value; }
            get { return m_dateTimeClientHeartBeatUTC; }
        }

        [DataMember]
        public Double? LastLocationLatitude
        {
            set { m_flLastLocationLatitude = value; }
            get { return m_flLastLocationLatitude; }
        }

        [DataMember]
        public Double? LastLocationLongitude
        {
            set { m_flLastLocationLongitude = value; }
            get { return m_flLastLocationLongitude; }
        }

        public override Dictionary<string, object> ToPropertyDictionary()
        {
            Dictionary<string, object> dictionaryProperties = new Dictionary<string, object>();

            if (DeviceID != null)
                dictionaryProperties.Add(KEY_DEVICEID, DeviceID.ToString());

            if (MemberID != null)
                dictionaryProperties.Add(KEY_MEMBERID, MemberID.ToString());

            if (ClientStarted != null)
                dictionaryProperties.Add(KEY_CLIENTSTARTDATE, ClientStarted);

            if (ClientHeartbeat != null)
                dictionaryProperties.Add(KEY_CLIENTHEARTBEAT, ClientHeartbeat);

            if (LastLocationLatitude != null)
                dictionaryProperties.Add(KEY_LASTLOCATIONLATITUDE, new Decimal((double)LastLocationLatitude));

            if (LastLocationLongitude != null)
                dictionaryProperties.Add(KEY_LASTLOCATIONLONGITUDE, new Decimal((double)LastLocationLongitude));

            if (LastLocationDateCollected != null)
                dictionaryProperties.Add(KEY_LASTLOCATIONDATECOLLECTED, LastLocationDateCollected);

            return dictionaryProperties;
        }
    }
}
